package com.fox.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.taglibs.standard.tag.el.sql.UpdateTag;

import com.fox.hiring.common.Constant;
import com.fox.hiring.common.util.AppUtil;
import com.fox.hiring.company.dao.ApplicationDao;
import com.fox.hiring.company.dao.PositionDao;
import com.fox.hiring.company.entity.ApplicationEntity;
import com.fox.hiring.company.entity.PositionEntity;
import com.fox.hiring.talent.dao.ResumeDao;
import com.fox.hiring.talent.entity.ResumeEntity;
import com.sun.xml.internal.bind.CycleRecoverable.Context;

/**
 * Position Servlet
 * 
 * @author fox.
 * @since 2016年2月26日
 */
public class PositionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private PositionDao positionDao = new PositionDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置POST请求的编码
		request.setCharacterEncoding("UTF-8");
		// 根据命令参数，决定调用哪个操作方法
		String command = request.getParameter("command");
		if ("listAllPosition".equals(command)) {
			listAllPosition(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else if("add".equals(command)){
			add(request,response);
		} else if ("apply".equals(command)) {
			apply(request, response);
		} else if("deletePosition".equals(command)){
			deletePosition(request,response);
		}else if("preUpdate".equals(command)){
			preUpdate(request,response);
		}else if("update".equals(command)){
			Update(request,response);
		} else {
			throw new RuntimeException("请求失败！");
		}
	}

	/**
	 * 显示全部的职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listAllPosition(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取参数；
		// 2、调用业务逻辑；
		List<PositionEntity> positionList = positionDao.listAllPosition();
		// 3、转向
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/position-list.jsp").forward(
				request, response);
	}

	/**
	 * 查询职位的详细信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void detail(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 1、获取参数；
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		// 2、调用业务逻辑；
		PositionEntity position = positionDao.get(positionId);
		// 3、转向
		if (position == null) {
			response.sendRedirect("PositionServlet?command=listAllPosition&result="
					+ Constant.OPERATE_ERROR);
		} else {
			// 查询所有的简历
			if (AppUtil.isLogin(request)) {
				int talentId = AppUtil.getTalentId(request, response);
				List<ResumeEntity> resumeList = resumeDao.list(talentId);
				request.setAttribute("resumeList", resumeList);

				// 判断该职位是否已申请
				ApplicationEntity application = applicationDao.isApply(
						positionId, talentId);
				request.setAttribute("application", application);
			}
			request.setAttribute("position", position);
			request.getRequestDispatcher("view/company/position-detail.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 职位申请
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void apply(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 1、获取参数；
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		// 2、调用业务逻辑；
		int talentId = AppUtil.getTalentId(request, response);
		int row = applicationDao.add(positionId, talentId, resumeId);
		// 3、转向
		if (row == 1) {
			response.sendRedirect("PositionServlet?command=detail&positionId="
					+ positionId + "&result=" + Constant.OPERATE_SUCCESS);
		} else {
			response.sendRedirect("PositionServlet?command=detail&positionId="
					+ positionId + "&result=" + Constant.OPERATE_ERROR);
		}
	}
	/**
	 * 发布职位
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException 
	 * @throws IOException 
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数
		int companyId = AppUtil.getCompanyId(request, response);
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		PositionEntity position = new PositionEntity();
		position.setCompanyId(companyId);
		position.setName(name);
		position.setDuty(duty);
		position.setAbility(ability);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		
		//调用业务逻辑
		int row = positionDao.add(position);
		//转向
		//成功转向->main.jsp
		if(row == 1){
		response.sendRedirect("CompanyServlet?command=main&result="+Constant.OPERATE_SUCCESS);
		
		//失败转向->position.jsp
		}else{
		request.setAttribute("position", position);
		request.setAttribute("result", Constant.OPERATE_ERROR);
		request.getRequestDispatcher("view/company/position-add.jsp").forward(request, response);
		}
	}

	/**
	 * 职位删除
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void deletePosition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//1、获取参数
		String positionId = request.getParameter("positionId");
		
		//2、调用业务逻辑
		int row = positionDao.deletePosition(positionId);
		//3、转向
		//成功->
		if(row == 1){
		response.sendRedirect("CompanyServlet?command=main&result="+Constant.OPERATE_SUCCESS);
		}else{
			response.sendRedirect("CompanyServlet?command=main&result="+Constant.OPERATE_ERROR);
		}
	}
	/**
	 * 职位修改前查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */

	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		//1、获取参数
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		//2、调用业务逻辑
		PositionEntity position = positionDao.get(positionId);
		//3、转向
		if(position == null){
			response.sendRedirect("CompanyServlet?command=main&result="+Constant.OPERATE_ERROR);
		}else{
			request.setAttribute("position", position);
			request.getRequestDispatcher("view/company/position-update.jsp").forward(request,response);
		}
	}
	/**
	 * 职位修改
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException 
	 * @throws IOException 
	 */
	private void Update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、获取参数
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		PositionEntity position = new PositionEntity();
		position.setId(positionId);
		position.setName(name);
		position.setDuty(duty);
		position.setAbility(ability);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		
		//2、调用业务逻辑
		int row = positionDao.update(position);
		//3、转向
		if(row == 1){
			response.sendRedirect("CompanyServlet?command=main&result="+Constant.OPERATE_SUCCESS);
		}else{
			request.setAttribute("result", Constant.OPERATE_ERROR);
			request.setAttribute("position", position);
			request.getRequestDispatcher("view/company/position-update.jsp").forward(request, response);
		}
	}
}
